package com.lvmama.rhino.web.comment.tags.controller;

import java.util.Map;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import com.github.pagehelper.PageInfo;
import com.lvmama.rhino.base.CookieUtil;
import com.lvmama.rhino.base.SerializeUtil;
import com.lvmama.rhino.pojo.comment.entity.CommentTag;
import com.lvmama.rhino.service.comment.TagsListService;
import com.lvmama.rhino.service.impl.comment.RedisServiceImpl;
import com.lvmama.rhino.utils.RhinoConstant;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.lvmama.entity.system.SysUser;

/**
 * @ClassName: TagsController
 * @Description: 评价指标增删改查方法
 * @author shenlinli
 * @date 2017年9月6日 上午11:59:12
 */
@Controller
@RequestMapping("tags")
public class TagsController {

	@Autowired
	private TagsListService tagsListService;

	private static Logger log = Logger.getLogger(TagsController.class);

    @Autowired
    private RedisServiceImpl redisService;

	@RequestMapping("list")
	public String tagsList(ModelMap model) {
		return "application/tagsPage/tagsList";
	}

	@RequestMapping("tagsAjaxData")
	@ResponseBody
	public Map<String, Object> tagsAjaxData(int pageSize, int pageNum) {
		PageInfo<CommentTag> pageInfo = tagsListService.findAllTagList(pageNum, pageSize);
		Map<String, Object> resultMap = new java.util.HashMap<>();
		resultMap.put("page", pageInfo);
		return resultMap;
	}

	/**
	 * 删除评价指标
	 *
	 * @param model
	 * @return
	 */
	@RequestMapping("deleteTag")
	public String deleteTag(ModelMap model, String tagId) {
		tagsListService.deleteById(tagId);
		model.addAttribute("message", "删除评价指标成功");
		return "application/tagsPage/tagsList";
	}

    /**
     * 增加评价指标
     *
     * @param model
     * @param request
     * @param tagName
     * @return
     */
    @RequestMapping("addTag")
    public String addTag(ModelMap model, HttpServletRequest request, String tagName) {
        try {
            Cookie cookie = CookieUtil.getCookieByName(request, "token");
            if (null == cookie) {
				RhinoConstant rhinoConstant = RhinoConstant.getInstance();
				String ssoServiceUrl = rhinoConstant.getValue("ssoServiceUrl");
                return "redirect:" + ssoServiceUrl;
            }
            String token = cookie.getValue();
            byte[] str = redisService.getByteArrayValue(token.getBytes());
            SysUser currentUser = (SysUser) SerializeUtil.deSerialize(str);
            tagsListService.addTag(tagName, currentUser.getUsername());
            model.addAttribute("message", "增加评价指标成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("tags addTag error:" + e.getMessage(), e);
        }
        return "application/tagsPage/tagsList";
    }

	/**
	 * 修改评价指标
	 *
	 * @param model
	 * @return
	 */
	@RequestMapping("modifyTag")
	public String modifyTag(ModelMap model, HttpServletRequest request, String tagName, String tagId) {
		tagsListService.modifyTag(tagName, tagId);
		model.addAttribute("message", "修改评价指标成功");
		return "application/tagsPage/tagsList";
	}

}
